<template>
  <div class="app-container">
      <el-row>
  <el-col :span="22">
    
     <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="合约" prop="symbol">
          <el-input v-model="filterForm.symbol" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
      </el-form>
  </el-col>
  <el-col :span="2">
    <el-button type="primary" @click="handleFilter">刷新</el-button>
  </el-col>
</el-row>
      <el-row>
  <el-col :span="12">
    <el-table 
      :data="policyList"
    >
      <el-table-column
        prop="symbol"
        label="合约">
      </el-table-column>
      <el-table-column
        prop="cycle"
        label="日期">
      </el-table-column>
      <el-table-column
        prop="direction"
        label="方向">
        <template slot-scope="{row}">
          <span v-if="row.direction == 1" style="color:red;">看多</span>
          <span v-else-if="row.direction == 2" style="color:green;">看空</span>
          <span v-else>未明确</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="signal"
        label="震荡">
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="发起时间">
      </el-table-column>
    </el-table>
  </el-col>
  <el-col :span="12">
    <div>
      <el-radio-group v-model="signalFilterForm.scale">
        <el-radio-button label="3">M3</el-radio-button>
        <el-radio-button label="60">H1</el-radio-button>
        <el-radio-button label="1440">D1</el-radio-button>
      </el-radio-group>
      <el-button @click="getSignalList()">刷新</el-button>
    </div>
      <el-table
      :data="signalList"
      border
      style="width: 100%">
      <el-table-column
        prop="symbol"
        label="合约">
      </el-table-column>
      <el-table-column
        prop="title"
        label="主题">
      </el-table-column>
      <el-table-column
        label="编码"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.magic }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="价格"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.marketPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="开仓"
        width="100">
        <template slot-scope="{row}">
          <span v-if="row.openStatus == 1" style="color:red;">待处理</span>
          <span v-else style="color:green">已处理</span>
        </template>
      </el-table-column>
      <el-table-column
        label="平仓"
        width="100">
        <template slot-scope="{row}">
          <span v-if="row.closeStatus == 1" style="color:red;">待处理</span>
          <span v-else style="color:green">已处理</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="创建时间"
        width="160">
      </el-table-column>
      </el-table>
  </el-col>
</el-row>
      
    
      <el-row>
  <el-col :span="12">
  </el-col>
  <el-col :span="12">
      <div>
      <el-radio-group  v-model="positionForm.status">
        <el-radio-button label="0">全部</el-radio-button>
        <el-radio-button label="6">已平仓</el-radio-button>
        <el-radio-button label="5">未平仓</el-radio-button>
      </el-radio-group>
      <el-button type="primary" @click="getPurchaseList()">刷新</el-button>
    </div>
      <el-table
      :data="purchaseList"
      border
      style="width: 100%">
       <el-table-column
      type="index"
      width="50">
    </el-table-column>
      <el-table-column
        prop="symbol"
        label="合约">
      </el-table-column>
      <el-table-column
        label="价格"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="quantity"
        label="持仓">
      </el-table-column>
      <el-table-column
        label="市场价"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.marketPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="浮动盈亏"
        width="100">
        <template slot-scope="{row}">
          <span>{{ row.profit }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="createdAt"
        label="创建时间"
        width="160">
      </el-table-column>
      </el-table>
  </el-col>
</el-row>
    <pagination v-show="total>0" :total="total" :page.sync="filterForm.page" :limit.sync="filterForm.size" @pagination="getSignalList" />

  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination
import {getSymbol} from '@/utils/symbol' // secondary package based on el-pagination
export default {
  name: 'faultinessAdmin',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      
      socket:"",
      tableKey: 0,
      dataType:"1",
      windowHeight:"800",
      allowedLoaded:false,
      allowedLoadedRealtime:false,
      signalFilterForm:{},
      symbolList:[],
      policyList:[],
      signalList:[],
      purchaseList:[],
      contractList: [
      ],
      total: 0,
      listLoading: false,
      connected:"loading",
      realtimeSetting:{
        scale:1,
        symbol:"i2309"
      },
      positionForm:{
        localSymbol:"i",
        status:5,
        siminute:2
      },
      filterForm: {
        page: 1,
        size: 10,
        localSymbol:"I",
        scale:60,
        symbol:''
      }
    }
  },
  created() {
    //开启行情自动读取
    this.allowedLoaded = true;
    this.allowedLoadedRealtime = true;
    let querySymbol = this.$route.query && this.$route.query.symbol
    if(querySymbol == undefined){
       querySymbol = window.localStorage.getItem("__local_symbol__")
    }
    if(querySymbol != undefined){
      this.filterForm.symbol = querySymbol
    }
    try {
      let symbolData = getSymbol()
      this.filterForm.localSymbol = symbolData.code
      this.positionForm.localSymbol = this.filterForm.localSymbol
      
    } catch (error) {
      
    }
    //this.init()
  },
  mounted(){
  },
  watch:{
    dataType(newCycle){
      if(newCycle == 1){
        this.getPolicyList()
      }else{
        this.getSignalList()
      }
    }
  },
  destroyed(){
    this.allowedLoaded = false
    this.allowedLoadedRealtime = false
    this.close()
  },
  methods: {
    init: function () {
      if(typeof(WebSocket) === "undefined"){
          alert("您的浏览器不支持socket")
      }else{
          // 实例化socket
          this.socket = new WebSocket(getHost())
          // 监听socket连接
          this.socket.onopen = this.open
          // 监听socket错误信息
          this.socket.onerror = this.error
          // 监听socket消息
          this.socket.onmessage = this.getMessage
      }
    },
    open: function () {
        let _this = this;
        this.connected = 'loaded'

        this.getSignalList()
        //this.getRealtimeList()
        this.getPolicyList()
        setTimeout(function(){
          _this.getPurchaseList()
        },3000)
    },
    error: function (e) {
        console.log("连接错误",JSON.stringify(e))
    },
    getMessage: function (receiveData) {
      let _this = this
      receiveData = JSON.parse(receiveData.data)
        console.log(receiveData)
      if(receiveData.status != 200){
          console.log(receiveData.msg)
      }else{
        let localUrl = window.location.href
        console.log(localUrl)
        if(receiveData.conversationType == "quotation.policy.list"){
          _this.policyList = receiveData.data.list

        }
        if(receiveData.conversationType == "position.purchase.list"){
          this.purchaseList = receiveData.data.list

        }
        if(receiveData.conversationType == "quotation.signal.list"){
          this.signalList = receiveData.data.list
          this.total = receiveData.data.total

        }
      }
    },
    send: function (params) {
      params = JSON.stringify(params)
      while(true){
        try {
            this.socket.send(params)
            break
        } catch (error) {
            //this.init()
            break           
        }

      }
    },
    close: function () {
        console.log("socket已经关闭")
    },
    destroyed () {
        // 销毁监听
        this.socket.onclose = this.close
    },
    getRealtimeList:function(){
       let _this = this
      if(_this.allowedLoadedRealtime){
        this.send({
          "conversationType":"quotation.realtime.list",
          "message":this.filterForm,
          "target":{"id":"3"}
        })
        setTimeout(function(){
          _this.getSignalList()
        },5000)
        _this.allowedLoadedRealtime = false
      }
      if(_this.allowedLoaded){
        /*
        setTimeout(function(){
          let _curDate = new Date()
          let _curHour = _curDate.getHours()
          _curHour = parseInt(_curHour)
          let _curMinute = _curDate.getMinutes()
          _curMinute = parseInt(_curMinute)
          console.log("策略面板",_curHour,_curMinute)
          if(_curMinute%10 == 0){
            let _curSeconds = _curDate.getSeconds()
            _curSeconds = parseInt(_curSeconds)
            if(_curSeconds%45 == 0){              
              if(!_this.allowedLoadedRealtime){
                _this.allowedLoadedRealtime = true
              }
            }
          }
          _this.getRealtimeList()
        },1000)
        */
      }
    },
    getContractList:function(){
      this.send({
        "conversationType":"quotation.market.list",
        "message":{"page":this.filterForm.page,"size":this.filterForm.size,"symbol":this.filterForm.localSymbol},
        "target":{"id":"3"}
      })
    },
    getPurchaseList:function(){
      this.send({
        "conversationType":"position.purchase.list",
        "message":this.positionForm,
        "target":{"id":"3"}
      })
    },
    getPolicyList:function(){
      this.send({
        "conversationType":"quotation.policy.list",
        "message":this.filterForm,
        "target":{"id":"3"}
      })
    },
    getSignalList:function(){
      this.send({
        "conversationType":"quotation.signal.list",
        "message":this.signalFilterForm,
        "target":this.signalFilterForm
      })
    },
    getList:function(){
      this.send({
        "conversationType":"quotation.symbol.list",
        "message":{"page":this.filterForm.page,"size":this.filterForm.size},
        "target":{"id":"3"}
      })
    },
    handleRefreshButton:function(){
        this.getList()
    },
    handleDetail:function(item){
      this.filterForm.symbol = item.code;
      this.getPolicyList()
    },
    handleFilter() {
      console.log("合约变动")
      window.localStorage.setItem("__local_symbol__",this.filterForm.symbol)
      this.filterForm.page = 1
      this.allowedLoadedRealtime = true
      this.getPolicyList()
    },
    sortChange(data) {
      const { prop, order } = data
      if (prop === 'id') {
        this.sortByID(order)
      }
    },
    sortByID(order) {
      if (order === 'ascending') {
        this.filterForm.sort = '+id'
      } else {
        this.filterForm.sort = '-id'
      }
      this.handleFilter()
    }
  }
}
</script>
<style>

</style>